package com.zpark.config;

import com.zpark.interceptors.UserInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 自定义MVC(定制SpringMVC)
 */
@Configuration
public class MyMVCConfig implements WebMvcConfigurer {

    /*
        注册拦截器
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 创建拦截器对象
        UserInterceptor userInterceptor = new UserInterceptor();
        // 获取拦截器注册对象
        InterceptorRegistration registration = registry.addInterceptor(userInterceptor);
        // 配置拦截规则
        List<String> blackList = new ArrayList<>();
        blackList.add("/book/**");
        blackList.add("/templates/**");

        // 添加拦截规则
        registration.addPathPatterns(blackList);

        // 配置不拦截规则
        List<String> whiteList = new ArrayList<>();
        whiteList.add("/book/login");
        whiteList.add("/book/index");
        whiteList.add("/templates/books/index.html");

        // 添加不拦截规则
        registration.excludePathPatterns(whiteList);
    }

    /*
        页面跳转
     */
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("demo01");
    }

    /*
       静态资源自定义访问
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/html/**").addResourceLocations("classpath:/html/");
    }
}
